﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using xfsale.lib;

namespace xfsale.ui
{
    public partial class WinLogin : WinBase
    {
        public WinLogin()
        {
            InitializeComponent();
        }

        private void WinPasswod_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Retry)
            {
                e.Cancel = true;
            }
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string regionCode = System.Configuration.ConfigurationSettings.AppSettings["RegionCode"];
            string branchCode = System.Configuration.ConfigurationSettings.AppSettings["BranchCode"];
            string netCode = System.Configuration.ConfigurationSettings.AppSettings["NetCode"];
            string opCode = TxtOpID.Text;
            string opPassword = TxtPassword.Text;

            if (!Regex.IsMatch(regionCode, @"^\d{4}$"))
            {
                SysMessage.Error("地区编号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(branchCode, @"^\d{8}$"))
            {
                SysMessage.Error("网点号码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(netCode, @"^\d{8}$"))
            {
                SysMessage.Error("销售网点号码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opCode, @"^\d{1,8}$"))
            {
                SysMessage.Error("用户帐号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opPassword, @"^\d{6}$"))
            {
                SysMessage.Error("用户密码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            try
            {
                App.Op.RegionCode = regionCode;
                App.Op.BranchCode = branchCode;
                App.Op.NetCode = netCode;
                App.Op.OpCode = opCode;
                App.Op.OpName = "";

                App.Sys.Status = "1";
                App.Sys.Version = "0.0.1";
                App.Sys.WorkDay = DateTime.Now.ToString("yyyyMMdd");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.Retry;
            }
        }
    }
}
